//
//  LGPatientBasicInfoController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/13.
//  基本信息

#import "LGPatientBasicInfoController.h"
//危险分层
#import "LGPatientRiskController.h"
//添加、编辑分组
#import "LGAddPatientGroupController.h"
//备注
#import "LGAddPatientRemarkController.h"

//签约历史
#import "LGPatientSignHistoryController.h"
#import "LGGetWebViewController.h"
//诊断
#import "LGBasicInfoDiagnoseCell.h"
#import "LGBasicInfoCommonCell.h"
#import "LGBasicInfoBodayCell.h"
#import "LGBaseInfoHeaderView.h"
#define viewHeight  (Screen_H-topBarHeight-viewPix(44))

@interface LGPatientBasicInfoController ()<UITableViewDelegate,UITableViewDataSource,LGBaseInfoHeaderViewDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSDictionary *basicDic;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGPatientBasicInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"基本信息";
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    [self.view addSubview:self.tableView];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"RefreshSurgeryData" object:nil];
    
    //服务包
    self.basicDic = @{@"title":@"",@"key":@"other",@"editKey":@"",@"timeKey":@"",@"items":@[
    @{@"title":@"服务包",@"key":@"vip",@"superKey":@"base"},
    @{@"title":@"我的分组",@"key":@"groups_name",@"superKey":@"other"},
    @{@"title":@"备注",@"key":@"bz",@"superKey":@"other"},
    @{@"title":@"主管医生",@"key":@"main_doctorname",@"superKey":@"base"}
    ]};
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    self.levelid = (self.levelid && ![self.levelid isEmptyString])?self.levelid:@"";
    [RequestUtil withPOST:@"/doctor/patient.profile/detailV2" parameters:@{@"patient_id":self.patientid,@"disease_code":self.diseaseCode,@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataDic = returnData;
            self.dataArray = returnData[@"datas"];
//            self.healthArray = returnData[@"jbxx"][@"detail"];
//            self.basicArray = returnData[@"renkx"][@"detail"];
//            self.bodayArray = returnData[@"bmi"][@"detail"];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//编辑基本信息
-(void)editBasicInfoAction:(NSString *)urlStr{
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.urlStr = urlStr;
    [self.navigationController pushViewController:controller animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count+1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        NSArray *tempArray = self.basicDic[@"items"];
        return tempArray.count;
    }else{
        NSArray *tempArray = self.dataArray[section-1][@"detail"];
        return tempArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGBasicInfoCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.vipLevel = @"";
    cell.status = @"";
    cell.showTip = NO;
    NSArray *tempArray = @[];
    if (indexPath.section == 0) {
        tempArray = self.basicDic[@"items"];
        NSDictionary *tempDic = self.basicDic[@"items"][indexPath.row];
        NSString *key = LGNSString(tempDic[@"key"]);
        NSString *superKey = LGNSString(tempDic[@"superKey"]);
        cell.title = LGNSString(tempDic[@"title"]);
        NSString *content = @"";
        if ([self.dataDic isKindOfClass:[NSDictionary class]] && [self.dataDic.allKeys containsObject:superKey]) {
            NSDictionary *superDic = self.dataDic[superKey];
            if ([superDic isKindOfClass:[NSDictionary class]] && [superDic.allKeys containsObject:key]) {
                if ([superDic[key] isKindOfClass:[NSArray class]]) {
                    //内容是个数组
                    NSArray *itemArray = superDic[key];
                    if ([key isEqualToString:@"shouss_fa"]) {
                        //手术史是个对象数组
                        for (NSDictionary *contentDic in itemArray) {
                            content = (content.length>0)?[NSString stringWithFormat:@"%@、%@ %@",content,contentDic[@"title"],contentDic[@"date"]]:[NSString stringWithFormat:@"%@ %@",contentDic[@"title"],contentDic[@"date"]];
                        }
                    }else{
                        //字符串数组
                        for (NSString *contentStr in itemArray) {
                            content = (content.length>0)?[NSString stringWithFormat:@"%@、%@",content,contentStr]:contentStr;
                        }
                    }
                }else{
                    //内容是字符串
                    content = LGNSString(superDic[key]);
                }
            }
            
            
            if(indexPath.row == 0){
                //服务包
                NSString *title = LGNSString(superDic[@"vip_title"]);
                NSString *time = LGNSString(superDic[@"vip_date"]);
                time = [time isEmptyString]?@"":[NSString stringWithFormat:@"%@到期",time];
                content = [title isEmptyString]?[NSString stringWithFormat:@"%@%@",title,time]:[NSString stringWithFormat:@"%@(%@)",title,time];
                cell.showTip = NO;
                if(content.length>0 && ![content isEqualToString:@"(null)"]){
                    cell.vipLevel = LGNSString(superDic[@"vip_level"]);
                    cell.content = content;
                    cell.contentLabel.textColor = [UIColor colorWithString:@"#282828"];
                    cell.contentLabel.font = LGFontWeight(16, UIFontWeightMedium);
                }else{
                    cell.vipLevel = @"";
                    cell.content = @"";
                    cell.contentLabel.textColor = [UIColor colorWithString:@"#909399"];
                    cell.contentLabel.font = LGFont(15);
                }
            }else if(indexPath.row == 1){
                //分组
                cell.showTip = YES;
                if(content.length>0 && ![content isEqualToString:@"(null)"]){
                    cell.content = content;
                    cell.contentLabel.textColor = [UIColor colorWithString:@"#282828"];
                    cell.contentLabel.font = LGFontWeight(16, UIFontWeightMedium);
                }else{
                    cell.content = @"添加分组";
                    cell.contentLabel.textColor = [UIColor colorWithString:@"#909399"];
                    cell.contentLabel.font = LGFont(15);
                }
            }else if(indexPath.row == 2){
                //备注
                cell.showTip = YES;
                if(content.length>0 && ![content isEqualToString:@"(null)"]){
                    cell.content = content;
                    cell.contentLabel.textColor = [UIColor colorWithString:@"#282828"];
                    cell.contentLabel.font = LGFontWeight(16, UIFontWeightMedium);
                }else{
                    cell.content = @"添加备注";
                    cell.contentLabel.textColor = [UIColor colorWithString:@"#909399"];
                    cell.contentLabel.font = LGFont(15);
                }
            }else{
                cell.showTip = NO;
                if(content.length>0 && ![content isEqualToString:@"(null)"]){
                    cell.content = content;
                    cell.contentLabel.textColor = [UIColor colorWithString:@"#282828"];
                    cell.contentLabel.font = LGFontWeight(16, UIFontWeightMedium);
                }else{
                    cell.content = @"";
                    cell.contentLabel.textColor = [UIColor colorWithString:@"#909399"];
                    cell.contentLabel.font = LGFont(15);
                }
            }
        }
    }else{
        //普通内容
        tempArray = self.dataArray[indexPath.section-1][@"detail"];
        NSDictionary *tempDic = tempArray[indexPath.row];
        cell.showTip = NO;
        cell.content = LGNSString(tempDic[@"value"]);
        cell.title = LGNSString(tempDic[@"title"]);
        cell.contentLabel.textColor = [UIColor colorWithString:@"#282828"];
        cell.contentLabel.font = LGFontWeight(16, UIFontWeightMedium);
    }
    
    if (indexPath.row == 0 && indexPath.section == 0) {
        cell.type = LGPositionTypeTop;
    }else{
        cell.type = LGPositionTypeMiddle;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 1){
        //分组
        LGAddPatientGroupController *controller = [[LGAddPatientGroupController alloc]init];
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (indexPath.section == 0 && indexPath.row == 2){
        //备注
        LGAddPatientRemarkController *controller = [[LGAddPatientRemarkController alloc]init];
        controller.patientid = self.patientid;
        controller.remark = LGNSString(self.dataDic[@"other"][@"bz"]);
        [self.navigationController pushViewController:controller animated:YES];
        __weak typeof(self) weakSelf = self;
        controller.patientRemarkUpdate = ^(NSString * _Nonnull remark) {
            [weakSelf requestData];
        };
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return 0.01;
    }else{
        return viewPix(45);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CGFloat height = (section == 0)?0.01:viewPix(45);
    LGBaseInfoHeaderView *headView = [[LGBaseInfoHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    if (section>0) {
        NSDictionary *tempDic = self.dataArray[section-1];
//        NSString *timeKey = LGNSString(tempDic[@"timeKey"]);
//        NSString *urlKey = LGNSString(tempDic[@"editKey"]);
//        NSString *superKey = LGNSString(tempDic[@"key"]);
//        NSDictionary *superDic = self.dataDic[superKey];
        headView.titleLabel.text = LGNSString(tempDic[@"title"]);
        headView.dateLabel.text = LGNSString(tempDic[@"update_time"]);
        headView.urlStr = LGNSString(tempDic[@"edit_url"]);
        headView.delegate = self;
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footView.bottomMargin = viewPix(8);
    return footView;
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGBasicInfoCommonCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
